/**
 * <b> 文件: </b> Add.java
 * @author xhe
 * @version V1.0.0.0
 * @version --2020-10-27
 */
package xhe.lib.data;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;
import xhe.entities.ApiEntity;
import xhe.enums.ExecuteEnum;
import xhe.enums.OperateEnum;
import xhe.lib.base.Common;
import xhe.lib.db.BaseFactory;

/**
 * <b> 类: </b> Add.java
 * 
 * @author xhe
 * @version V1.0.0.0
 * @version --2020-10-27
 */
public class Add {

	/**
	 * <b> 接口: </b> 通用添加接口
	 * 
	 * @author xhe
	 * @param requestEntity 接口属性类
	 * @return ApiEntity 接口属性类
	 * @version V1.0.0.0
	 * @version --2020年5月26日
	 * @version --新增方法
	 */
	public ApiEntity Add_1_0(ApiEntity requestEntity) {

		try {
			String strParams = "";

			// 解析请求参数
			if (StringUtils.isEmpty(requestEntity.getParams())) {
				requestEntity.setCode(ExecuteEnum.RequestError);
				requestEntity.setMessage("请求参数不能为空!");
			} else {
				strParams = requestEntity.getParams();
			}

			BaseFactory oBaseFactory = new BaseFactory(requestEntity);

			// System.out.println(strParams);

			// 解析请求串(json)
			JSONObject jsonObject = JSONObject.fromObject(strParams);
			// 获取表名
			String strTable = jsonObject.getString("Table");

			if (strTable.contains(" ")) {
				strTable = strTable.split(" ")[0];
			}

			// 验证表名不允许为空
			if (StringUtils.isEmpty(strTable)) {
				requestEntity.setCode(ExecuteEnum.RequestError);
				requestEntity.setMessage("表名不能为空!");
			}

			Boolean hasData = false;

			Map<Integer, List<String>> strFilterList = Util.getFilterList(strParams);
			// 查询条件
			// String strFilter = Util.getFilter(strParams);
			String strFilter = Util.getFilter(strFilterList);

			if (StringUtils.isNotEmpty(strFilter)) {
				String strSelectSql = "select count(1) as rowCount from " + strTable + " where 1=1 " + strFilter;

				System.out.println("SelectSql:" + strSelectSql);

				// 查询数据
				ResultSet rs = oBaseFactory.Query(strSelectSql, requestEntity);
				rs.next();
				int rowCount = rs.getInt("rowCount");

				System.out.println("RowCount:" + rowCount);

				if (rowCount > 0) {
					hasData = true;
				}
			}

			// 返回结果
			Boolean blResult = false;
			String strResult = "";
			String strContent = "";

			if (hasData) {

				// 封装返回结果
				strResult += "{";
				strResult += "\"result\": false,";
				strResult += "\"rows\": 0,";
				strResult += "\"message\": \"已添加过重复数据!\"";
				strResult += "}";

				strContent = "已添加过重复数据!";

			} else {
				Map<String, List<String>> strColumnList = Util.getColumnList(strParams);

				// 查询语句
				String strColumns = "";
				String strValues = "";

				for (String strColumn : strColumnList.keySet()) {
					String strValue = strColumnList.get(strColumn).get(0);
					String strType = strColumnList.get(strColumn).get(1);
					strContent += strColumn + ":" + strValue + ";";
					// String strFormat = strColumnList.get(strColumn).get(2);
					String strDefault = strColumnList.get(strColumn).get(3);

					strColumns += strColumn + ",";

					System.out.println("Column:" + strColumn + ",Type:" + strType + ",Value:" + strValue);

					if (strType.equals("D")) {
						if (StringUtils.isEmpty(strValue)) {
							if (StringUtils.isEmpty(strDefault)) {
								strValues += "null,";
							} else {
								strValues += strDefault + ",";
							}
						} else {
							strValues += "'" + strValue + "',";
						}
					} else if (strType.equals("B")) {
						if (StringUtils.isEmpty(strValue)) {
							if (StringUtils.isEmpty(strDefault)) {
								strValues += "false,";
							} else {
								strValues += strDefault + ",";
							}
						} else {
							strValues += Common.toPraseBoolean(strValue, false) + ",";
						}
					} else if (strType.equals("F") || strType.equals("I")) {
						if (StringUtils.isEmpty(strValue)) {
							if (StringUtils.isEmpty(strDefault)) {
								strValues += "null,";
							} else {
								strValues += strDefault + ",";
							}
						} else {
							strValues += strValue + ",";
						}
					} else if (strType.equals("P")) {

						strValues += "'" + Util.getMaxPrimaryKey(requestEntity, strTable, strColumn, strValue) + "',";

					} else {
						if (StringUtils.isEmpty(strDefault)) {
							strValues += "'" + strValue + "',";
						} else {
							strValues += "'" + strDefault + "',";
						}
					}
				}

				if (strColumns != "") {
					strColumns = strColumns.substring(0, strColumns.length() - 1);
				}

				if (strValues != "") {
					strValues = strValues.substring(0, strValues.length() - 1);
				}

				String strSql = "insert into " + strTable + "(" + strColumns + ") values (" + strValues + ")";

				System.out.println("SQL:" + strSql);

				// 执行语句
				int iResult = oBaseFactory.Execute(strSql, requestEntity);

				// 封装返回对象
				if (iResult > 0) {

					blResult = true;
				}

				// 封装返回结果
				strResult += "{";
				strResult += "\"result\": " + blResult + ",";
				strResult += "\"rows\": " + iResult + ",";
				strResult += "\"message\": \"" + requestEntity.getMessage() + "\"";
				strResult += "}";
			}

			requestEntity.setReturnType(false);
			requestEntity.setResult(strResult);
			requestEntity.setTableName(strTable);
			requestEntity.setContent(strContent);
			requestEntity.setOperateType(OperateEnum.Add.getCode());

			try {
				requestEntity.setPrimaryKey(strFilterList.get(0).get(0));
				requestEntity.setPrimaryValue(strFilterList.get(0).get(1));
			} catch (Exception ex) {
			}

		} catch (Exception ex) {

			requestEntity.setCode(ExecuteEnum.Error);
			requestEntity.setMessage(ex.getMessage());
		}

		return requestEntity;
	}

}
